package com.hankol.feature_wine_meal

import android.widget.LinearLayout
import androidx.lifecycle.Observer
import androidx.viewpager.widget.ViewPager
import com.baselibrary.activity.BaseActivity
import com.baselibrary.manager.LoadingManager
import com.baselibrary.router.ActivityRouter
import com.baselibrary.utils.ScreenUtils
import com.hankol.feature_wine_meal.adapter.CuisineVpAdapter
import com.hankol.feature_wine_meal.api.WineMealHelp
import com.hankol.feature_wine_meal.bean.CuisineRefreshBean
import com.hankol.feature_wine_meal.databinding.ActivityCuisineBinding
import com.hankol.feature_wine_meal.viewmodel.CuisineViewModel

/**
 * @description 特色菜系
 * @author 关忆北、
 * @time 2022/6/20 9:51 上午
 */
class CuisineActivity : BaseActivity() {
    private lateinit var mDataBinding: ActivityCuisineBinding
    private lateinit var mViewModel: CuisineViewModel
    private val adapter: CuisineVpAdapter by lazy { CuisineVpAdapter() }
    private var currentPage: Int = -1
    var changeBannerListBean: ArrayList<CuisineRefreshBean> = arrayListOf()//改变后的数据
    var titleList: ArrayList<String> = arrayListOf()//title集合
    private var mCuisineRefreshBean: ArrayList<CuisineRefreshBean>? = null


    override fun getLayoutId(): Int {
        return R.layout.activity_cuisine
    }

    override fun init() {
        mDataBinding = getDataBinding<ActivityCuisineBinding>()
        mViewModel = getActivityViewModelProvider(this).get(CuisineViewModel::class.java)
        initData()
        initListener()
        initObserve()
        val layoutParams: LinearLayout.LayoutParams = mDataBinding.tabCuisineTitle.layoutParams as LinearLayout.LayoutParams
        layoutParams.width = ScreenUtils.dip2px(applicationContext, 10f * 6f) + ScreenUtils.dip2px(applicationContext, 36f * 3f) + ScreenUtils.dip2px(applicationContext, 9f)
        mDataBinding.tabCuisineTitle.layoutParams = layoutParams


        val params: LinearLayout.LayoutParams = mDataBinding.vpCuisine.layoutParams as LinearLayout.LayoutParams
        params.height = mViewModel.changeHeight(this)
        mDataBinding.vpCuisine.layoutParams = params

    }

    private fun initListener() {
        mDataBinding.smartCuisine.setEnableLoadMore(false)
        mDataBinding.llCuisineBack.setOnClickListener { finish() }
        mDataBinding.llCuisineIntroduce.setOnClickListener {
            ActivityRouter.startActivity(ActivityRouter.WINE_MEAL.WINE_MEAL_INTRO)
        }
        mDataBinding.tvCuisineChange.setOnClickListener {
            mCuisineRefreshBean?.let {
                if (currentPage != -1) {
                    LoadingManager.showLoadingDialog(this)

                    mViewModel.reqChangeBatch(it[currentPage].appetizerCategory.id)
                }
            }
        }

        mDataBinding.smartCuisine.setOnRefreshListener {
            mViewModel.reqCuisine()
        }

        mDataBinding.vpCuisine.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                currentPage = position
//                (position - 1 + titleList.size) % titleList.size
                mViewModel.upTabLayout(applicationContext, position, mDataBinding.tabCuisineTitle)
            }

            override fun onPageScrollStateChanged(state: Int) {

                //验证当前的滑动是否结束
                if (state == ViewPager.SCROLL_STATE_IDLE) {
                    //滑动动画做完的时候
                    if (currentPage == 0) {
                        mDataBinding.vpCuisine.setCurrentItem(changeBannerListBean.size - 2, false) //切换，不要动画效果
                    } else if (currentPage == changeBannerListBean.size - 1) {
                        mDataBinding.vpCuisine.setCurrentItem(1, false) //切换，不要动画效果
                    }
                }
            }
        })
    }

    fun initData() {
        mDataBinding.vpCuisine.adapter = adapter
        mDataBinding.vpCuisine.pageMargin = ScreenUtils.dpToPx(10)
        mViewModel.reqCuisine()
    }

    private fun initObserve() {
        mViewModel.refreshCuisineLiveData.observe(this, Observer { it ->
            mDataBinding.smartCuisine.finishRefresh()
            if (it != null && it.isNotEmpty()) {
                changeBannerListBean.clear()
                changeBannerListBean.addAll(it)
                changeBannerListBean.add(it[0])
                changeBannerListBean.add(0, it[it.size - 1])
                adapter.setData(changeBannerListBean)
                titleList.clear()
                it.forEach {
                    titleList.add(it.appetizerCategory.name)
                }
                titleList.add(it[0].appetizerCategory.name)
                titleList.add(0, it[it.size - 1].appetizerCategory.name)
                adapter.setTitles(titleList)
                mDataBinding.tabCuisineTitle.setViewPager(mDataBinding.vpCuisine, titleList.toTypedArray())
                if (currentPage != 1) {
                    mDataBinding.vpCuisine.setCurrentItem(1, true)
                } else {
                    mViewModel.upTabLayout(applicationContext, 1, mDataBinding.tabCuisineTitle)
                }
            }
            mCuisineRefreshBean = changeBannerListBean//将最终的数据进行赋值
        })

        mViewModel.changeBatchCuisineLiveData.observe(this, Observer {
            LoadingManager.hideLoadingDialog(this)
            if (it.isNotEmpty()) {
                adapter.hasMap[currentPage]?.setNewData(it)
            }
        })
    }
}